﻿using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;

using System.Web;

namespace nadModel
{
    public class DbConnectionHelper
    {
 
        private string sqlConfig;
        private string linkedServer;

        private DbConnectionHelper(string sqlConfig, string linkedServer)
        {
            this.sqlConfig = sqlConfig;
            this.linkedServer = linkedServer;
        }

        public static byte[] Compress(byte[] toCompress)
        {

            using (ZipFile file = new ZipFile())
            {

                file.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

                file.AddEntry("temp.obj", toCompress);

                MemoryStream outputStream = new MemoryStream();
                file.Save(outputStream);

                return outputStream.ToArray();
            }

        }

        public static byte[] Decompress(Stream stream)
        {
            return GetByte(ZipFile.Read(stream));
        }

        private static byte[] GetByte(ZipFile A_0)
        {
            MemoryStream stream = new MemoryStream();
            using (IEnumerator<ZipEntry> enumerator = A_0.GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    enumerator.Current.Extract(stream);
                }
            }
            return stream.ToArray();
        }

        public static byte[] Decompress(byte[] toDecompress)
        {
            return GetByte(ZipFile.Read(toDecompress));
        }

        public static string FieldNameToPropertyName(string dbName)
        {
            StringBuilder builder = new StringBuilder();
            bool flag = false;
            for (int i = 0; i < dbName.Length; i++)
            {
                if (dbName[i] == '_')
                {
                    flag = true;
                }
                else if ((i != 0) && !flag)
                {
                    builder.Append(dbName[i]);
                }
                else
                {
                    char ch = dbName[i];
                    builder.Append(ch.ToString().ToUpper());
                    flag = false;
                }
            }
            return builder.ToString();
        }

        public string GetConnectionString()
        {
            return this.sqlConfig;
        }

        public static DbConnectionHelper GetInstance()
        {
            return null;
        }

        public static string SqlEscape(string s)
        {
            return s.Replace("'", "''");
        }

        // Properties
        public string LinkedServer
        {
            get
            {
                return this.linkedServer;
            }
        }

    }
}
